package top.fengleifeng.question;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author : fengelfeing
 * @version V1.0
 * @Project: leetcode-test
 * @Package : top.fengleifeng.question
 * @Description:
 * @Date: 2020年11月17日11:14 上午
 */
public class Num1405最长快乐字符串 {

    /**
     * 测试 贪心算法 添加多的， 前两个一样添加次多的
     */
    class Solution {

        public String longestDiverseString(int a, int b, int c) {
            Map<Character, Integer> map = new HashMap<>();
            map.put('a', a);
            map.put('b', b);
            map.put('c', c);
            int allCount = a + b + c;
            StringBuffer sb = new StringBuffer("");
            while (true) {
                if (sb.length() >= 2) {
                    char preOne = sb.charAt(sb.length() - 1);
                    char preTwo = sb.charAt(sb.length() - 2);
                    if (preOne == preTwo) {
                        Character maxChar = geTwoMax(map, preOne);
                        if (Objects.isNull(maxChar)) {
                            return sb.toString();
                        }
                        sb.append(maxChar);
                    } else {
                        Character maxChar = getMax(map);
                        if (Objects.isNull(maxChar)) {
                            return sb.toString();
                        }
                        sb.append(maxChar);
                    }
                } else {
                    Character maxChar = getMax(map);
                    if (Objects.isNull(maxChar)) {
                        return sb.toString();
                    }
                    sb.append(maxChar);
                }
            }
        }

        private Character geTwoMax(Map<Character, Integer> map, char preOne) {
            Character ac = 'a';
            Character bc = 'b';
            Character cc = 'c';
            Integer a = map.get(ac);
            Integer b = map.get(bc);
            Integer c = map.get(cc);

            if (bc.charValue() == preOne) {
                if (a >= c) {
                    if (a - 1 < 0) {
                        return null;
                    }
                    map.put(ac, map.get(ac) - 1);
                    return ac;
                } else {
                    if (c - 1 < 0) {
                        return null;
                    }
                    map.put(cc, map.get(cc) - 1);
                    return cc;
                }
            }

            if (ac.charValue() == preOne) {
                if (b >= c) {
                    if (b - 1 < 0) {
                        return null;
                    }
                    map.put(bc, map.get(bc) - 1);
                    return bc;
                } else {
                    if (c - 1 < 0) {
                        return null;
                    }
                    map.put(cc, map.get(cc) - 1);
                    return cc;
                }
            }
            if (cc.charValue() == preOne) {
                if (a >= b) {
                    if (a - 1 < 0) {
                        return null;
                    }
                    map.put(ac, map.get(ac) - 1);
                    return ac;
                } else {
                    if (b - 1 < 0) {
                        return null;
                    }
                    map.put(bc, map.get(bc) - 1);
                    return bc;
                }
            }
            return null;
        }

        private Character getMax(Map<Character, Integer> map) {
            Character ac = 'a';
            Character bc = 'b';
            Character cc = 'c';
            Integer a = map.get(ac);
            Integer b = map.get(bc);
            Integer c = map.get(cc);
            if (a >= b) {
                if (a >= c) {
                    if (a - 1 < 0) {
                        return null;
                    }
                    map.put(ac, map.get(ac) - 1);
                    return ac;
                } else {
                    if (c - 1 < 0) {
                        return null;
                    }
                    map.put(cc, map.get(cc) - 1);
                    return cc;
                }
            } else {
                if (b >= c) {
                    if (b - 1 < 0) {
                        return null;
                    }
                    map.put(bc, map.get(bc) - 1);
                    return bc;
                } else {
                    if (c - 1 < 0) {
                        return null;
                    }
                    map.put(cc, map.get(cc) - 1);
                    return cc;
                }
            }
        }

    }

    public static void main(String[] args) {
        int a = 1;
        int b = 1;
        int c = 7;
        String s = new Num1405最长快乐字符串().new Solution().longestDiverseString(a, b, c);

        System.out.println(s);
    }
}
